package com.rms.hkpc.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rms.hkpc.R;
import com.rms.hkpc.activity.base.BaseActivity;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.apiresult.LoginData;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.control.EditTextDRClick;
import com.rms.hkpc.db.dto.UserDto;
import com.rms.hkpc.presenter.LoginPresenter;
import com.rms.hkpc.util.CaptchaUtil;
import com.rms.hkpc.util.NetworkUtil;
import com.rms.hkpc.util.SPUtil;
import com.rms.hkpc.util.WindowUtil;
import com.rms.hkpc.view.ILoginView;

public class LoginActivity extends BaseActivity<ILoginView, LoginPresenter> implements ILoginView {

    private EditText etName, etCode;
    private EditTextDRClick etPassword;
    private TextView tvCode;
    private String code;
    private Button btnSignIn;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        WindowUtil.getInstance().setWindowDecorView(this);
        OnClickListener onClickListener = new OnClickListener();
//        用户名
        etName = fd(R.id.et_name);
        etName.setText(SPUtil.getInstance().getString(AllConstants.USER));
//        密码
        etPassword = fd(R.id.et_password);
//        验证码显示
        tvCode = fd(R.id.tv_code);
        showCode();
//        刷新验证码
        fd(R.id.iv_refresh).setOnClickListener(onClickListener);
//        输入验证码
        etCode = fd(R.id.et_code);
//        登录
        btnSignIn = fd(R.id.btn_signin);
        btnSignIn.setOnClickListener(onClickListener);
//        忘记密码
        fd(R.id.tv_forgotpw).setOnClickListener(onClickListener);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public String getUserName() {
        return etName.getText().toString();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString();
    }

    @Override
    public void onLoginSuccess(ApiResultData<LoginData> apiResultData) {
        btnSignIn.setEnabled(true);
        if (apiResultData.getStatus() != 2000) {
            Toast.makeText(this, apiResultData.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
        UserDto userInfo = apiResultData.getData().getUserInfo();
        SPUtil.getInstance().putString(AllConstants.USER,userInfo.getUserName());
        SPUtil.getInstance().putString(AllConstants.ID, userInfo.getId());
        SPUtil.getInstance().putString(AllConstants.TOKEN,
                apiResultData.getData().getToken() == null ? "" : apiResultData.getData().getToken());
        SPUtil.getInstance().putString(AllConstants.REFRESH_TOKEN,
                apiResultData.getData().getRefreshToken() == null ? "" : apiResultData.getData().getRefreshToken());
        if (userInfo.getProject() != null) {
            SPUtil.getInstance().putString(AllConstants.PROJECT_ID,
                    userInfo.getProject().getId() == null ?
                            "" : userInfo.getProject().getId());
            SPUtil.getInstance().putString(AllConstants.PROJECT_NAME,
                    userInfo.getProject().getName() == null ? "" :
                            userInfo.getProject().getName());
        }
        try {
            SPUtil.getInstance().putInt(AllConstants.LANGUAGE,
                    userInfo.getLang() == null ? 1 :
                            Integer.parseInt(userInfo.getLang().getId()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onLoginFail(Exception e) {
        btnSignIn.setEnabled(true);
        Toast.makeText(this, R.string.network_error, Toast.LENGTH_LONG).show();
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                // 刷新验证码
                case R.id.iv_refresh:
                    showCode();
                    break;
                // 登录
                case R.id.btn_signin:
                    if (TextUtils.isEmpty(getUserName())) {
                        Toast.makeText(LoginActivity.this, R.string.username_hint, Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (TextUtils.isEmpty(getPassword())) {
                        Toast.makeText(LoginActivity.this, R.string.pw_hint, Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (etCode.getText().toString().equalsIgnoreCase(code)) {
                        if (NetworkUtil.getInstance().isConnected()) {
                            v.setEnabled(false);
                            mPresenter.login();
                        } else {
                            Toast.makeText(LoginActivity.this, R.string.network_error, Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(LoginActivity.this, R.string.code_fail, Toast.LENGTH_LONG).show();
                    }
                    break;
                // 忘记密码
                case R.id.tv_forgotpw:
                    startActivity(new Intent(LoginActivity.this, ForgotPwActivity.class));
                    break;
            }
        }
    }

    /**
     * 生成验证码并展示
     */
    private void showCode() {
        code = CaptchaUtil.getInstance().getRandomCode(4);
        tvCode.setText(code);
    }
}